/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import me.zhengjie.modules.system.domain.DccQunzhongtianyan;
import me.zhengjie.modules.system.domain.DccXiaoxi;
import me.zhengjie.modules.system.repository.DccXiaoxiRepository;
import me.zhengjie.modules.system.service.mapstruct.DccXiaoxiMapper;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.DccQunzhongtianyanRepository;
import me.zhengjie.modules.system.service.DccQunzhongtianyanService;
import me.zhengjie.modules.system.service.dto.DccQunzhongtianyanDto;
import me.zhengjie.modules.system.service.dto.DccQunzhongtianyanQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.DccQunzhongtianyanMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author cjl
* @date 2023-05-17
**/
@Service
@RequiredArgsConstructor
public class DccQunzhongtianyanServiceImpl implements DccQunzhongtianyanService {

    private final DccQunzhongtianyanRepository dccQunzhongtianyanRepository;
    private final DccQunzhongtianyanMapper dccQunzhongtianyanMapper;
    private final DccXiaoxiRepository dccXiaoxiRepository;
    private final DccXiaoxiMapper dccXiaoxiMapper;

    @Override
    public Map<String,Object> queryAll(DccQunzhongtianyanQueryCriteria criteria, Pageable pageable){
        Page<DccQunzhongtianyan> page = dccQunzhongtianyanRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dccQunzhongtianyanMapper::toDto));
    }

    @Override
    public List<DccQunzhongtianyanDto> queryAll(DccQunzhongtianyanQueryCriteria criteria){
        return dccQunzhongtianyanMapper.toDto(dccQunzhongtianyanRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DccQunzhongtianyanDto findById(Integer id) {
        DccQunzhongtianyan dccQunzhongtianyan = dccQunzhongtianyanRepository.findById(id).orElseGet(DccQunzhongtianyan::new);
        ValidationUtil.isNull(dccQunzhongtianyan.getId(),"DccQunzhongtianyan","id",id);
        return dccQunzhongtianyanMapper.toDto(dccQunzhongtianyan);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DccQunzhongtianyanDto create(DccQunzhongtianyan resources) {
        return dccQunzhongtianyanMapper.toDto(dccQunzhongtianyanRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DccQunzhongtianyan resources) {
        DccQunzhongtianyan dccQunzhongtianyan = dccQunzhongtianyanRepository.findById(resources.getId()).orElseGet(DccQunzhongtianyan::new);
        ValidationUtil.isNull( dccQunzhongtianyan.getId(),"DccQunzhongtianyan","id",resources.getId());
        dccQunzhongtianyan.copy(resources);
        dccQunzhongtianyanRepository.save(dccQunzhongtianyan);
        if (1 == dccQunzhongtianyan.getStatus()){
            DccXiaoxi dccXiaoxi =new DccXiaoxi();
            dccXiaoxi.setCommonId(resources.getId());
            dccXiaoxi.setType(3);
            dccXiaoxi.setTitle("线索核实");
            dccXiaoxi.setUid(dccQunzhongtianyan.getOperator());
            dccXiaoxi.setStatus(0);
            dccXiaoxi.setText("线索已处理");
            dccXiaoxi.setOperator(resources.getOperator());
            dccXiaoxiMapper.toDto(dccXiaoxiRepository.save(dccXiaoxi));
        }
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            dccQunzhongtianyanRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<DccQunzhongtianyanDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DccQunzhongtianyanDto dccQunzhongtianyan : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("状态0刚发布未审核，1已通过，2已失败", dccQunzhongtianyan.getStatus());
            map.put("线索分类", dccQunzhongtianyan.getType());
            map.put("描述", dccQunzhongtianyan.getDccText());
            map.put("图片列表", dccQunzhongtianyan.getDccImglist());
            map.put("视频列表", dccQunzhongtianyan.getDccVediolist());
            map.put("联系方式", dccQunzhongtianyan.getContract());
            map.put("操作者", dccQunzhongtianyan.getOperator());
            map.put(" createdAt",  dccQunzhongtianyan.getCreatedAt());
            map.put(" updatedAt",  dccQunzhongtianyan.getUpdatedAt());
            map.put("外链", dccQunzhongtianyan.getOutUrl());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}